package com.hframe.data;

import java.lang.reflect.Field;


public class DRef<T extends DRef<?>> implements Base {

    public DRef() {
    }

    public boolean isEmpty() {
        try {
            Class<?> cla = this.getClass();
            while (null != cla && cla != DRef.class) {
                for (Field field : cla.getDeclaredFields()) {
                    field.setAccessible(true);
                    if (isExtendsClass(field.getType(), com.hframe.type.T.class)) {
                        field.setAccessible(false);
                        Object type = field.get(this);
                        if (null != type && ((com.hframe.type.T<?>) type).isEmpty())
                            return false;
                    }
                }
                cla = cla.getSuperclass();
            }
        } catch (Exception e) {
        }
        return true;
    }

    public static boolean isExtendsClass(Class<?> src, Class<?> dis) {
        while (src != null) {
            if (src == dis)
                return true;
            src = src.getSuperclass();
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuffer ret = new StringBuffer();
        try {
            Class<?> cla = this.getClass();
            while (null != cla && cla != DRef.class) {
                for (Field field : cla.getDeclaredFields()) {
                    field.setAccessible(true);
                    if (isExtendsClass(field.getType(), com.hframe.type.T.class)) {
                        Object type = field.get(this);
                        if (null == type) {
                            ret.append("\"").append(field.getName()).append("\"").append(":").append("null,");
                        } else {
                            ret.append("\"").append(field.getName()).append("\"").append(":").append(((com.hframe.type.T<?>) type).toString()).append(",");
                        }
                    }
                }
                cla = cla.getSuperclass();
            }
        } catch (Exception e) {
        }
        if (1 < ret.length())
            return "{" + ret.substring(1, ret.length() - 1) + "}";
        else
            return null;
    }
}